package org.mineground.commands.player;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.mineground.Main;
import org.mineground.PermissionNodes;
import org.mineground.Utilities;

public class TP implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) throws CommandException {
	Player player = (Player) sender;
        
        if (!player.hasPermission(PermissionNodes.PERMISSION_TP_COMMAND)) {
            return true;
        }

	if (args.length == 0) {
	    player.sendMessage(ChatColor.RED + "* Usage: '/tp <Player/On/Off/Accept/Deny>'");
	    return true;
	}

	if (args[0].equalsIgnoreCase("accept")) {
	    tpAccept(player);
	    return true;
	}

	else if (args[0].equalsIgnoreCase("deny")) {
	    tpDeny(player);
	    return true;
	}

	else if (args[0].equalsIgnoreCase("on")) {
	    if (Main.getInstance().getPlayer(player).getTeleportToggle()) {
		player.sendMessage(ChatColor.RED + "* Players can already TP to your position.");
		return true;
	    }

	    Main.getInstance().getPlayer(player).setTeleportToggle(true);

	    player.sendMessage(ChatColor.DARK_GREEN + "* Other players are now allowed to teleport to your position.");
	    return true;
	}

	else if (args[0].equalsIgnoreCase("off")) {
	    if (!Main.getInstance().getPlayer(player).getTeleportToggle()) {
		player.sendMessage(ChatColor.RED + "* Teleporting to your position has already been disabled.");
		return true;
	    }

	    Main.getInstance().getPlayer(player).setTeleportToggle(false);

	    player.sendMessage(ChatColor.DARK_GREEN + "* Other players are now not allowed to teleport to your position anymore.");
	    return true;
	}

	Player recv = Main.getInstance().getServer().getPlayer(args[0]);

	if (recv == null) {
	    player.sendMessage(ChatColor.RED + "* Error: Invalid player.");
	    return true;
	}

        if (!Main.getInstance().getPlayer(recv).getTeleportToggle() && !player.hasPermission(PermissionNodes.PERMISSION_TP_OVERRIDE)) {
            player.sendMessage(ChatColor.RED + "* Error: This player disabled teleporting to him/her.");
            return true;
        }
        
        if (!player.hasPermission(PermissionNodes.PERMISSION_TP_NOREQUEST)) {
            tpRequest(player, recv);
            return true;
        }
        
        if (recv.getWorld() == Main.getInstance().getServer().getWorld(Main.getInstance().getConfigHandler().survivalWorldName)) {
            player.sendMessage(ChatColor.RED + "* Error: That player is in the survival world right now and you are not allowed to teleport to him/her.");
            return true;
        }

        tp(player, recv);
	return true;

    }

    private void tp(Player player, Player recv) {
        if (player.equals(recv)) {
            player.sendMessage(ChatColor.RED + "* You can't teleport to yourself, silly.");
            return;
        }

	StringBuilder output = new StringBuilder();
	output.append(Colors.BROWN);
	output.append("* ");
	output.append(Utilities.fixName(player));
	output.append(Colors.BROWN);
	output.append(" has been teleported to ");
	output.append(Utilities.fixName(recv));
	output.append(Colors.BROWN);
	output.append(".");

	Main.getInstance().getIRCHandler().sendMessage("@" + Main.getInstance().getConfigHandler().ircChannel, output.toString());

	player.teleport(recv);

	recv.sendMessage(ChatColor.DARK_GREEN + "* " + player.getDisplayName() + " has been teleported to you.");
	player.sendMessage(ChatColor.DARK_GREEN + "* You have been teleported to " + recv.getDisplayName());
    }

    private void tpAccept(Player player) {
	if (!Main.getInstance().containsTeleport(player)) {
	    player.sendMessage(ChatColor.RED + "* No player requested to teleport to your position.");
	    return;
	}

	Player sender = Main.getInstance().getTeleportRequest(player);

	if (player.equals(sender)) {
	    player.sendMessage(ChatColor.RED + "* You can't teleport to yourself, silly.");
	    return;
	}

	StringBuilder output = new StringBuilder();
	output.append(Colors.BROWN);
	output.append("* ");
	output.append(Utilities.fixName(sender));
	output.append(Colors.BROWN);
	output.append(" has been teleported to ");
	output.append(Utilities.fixName(player));
	output.append(Colors.BROWN);
	output.append(".");

	Main.getInstance().getIRCHandler().sendMessage("@" + Main.getInstance().getConfigHandler().ircChannel, output.toString());

	sender.teleport(player);

	player.sendMessage(ChatColor.DARK_GREEN + "* You accepted " + sender.getDisplayName() + "'s request.");
	sender.sendMessage(ChatColor.DARK_GREEN + "* " + player.getDisplayName() + " accepted your request.");

	player.sendMessage(ChatColor.DARK_GREEN + "* " + sender.getDisplayName() + " has been teleported to you.");
	sender.sendMessage(ChatColor.DARK_GREEN + "* You have been teleported to " + player.getDisplayName());

	Main.getInstance().removeTeleportRequest(player);
    }

    private void tpDeny(Player player) {
	if (!Main.getInstance().containsTeleport(player)) {
	    player.sendMessage(ChatColor.RED + "* No player requested to teleport to your position.");
	    return;
	}

	Player sender = Main.getInstance().getTeleportRequest(player);

	player.sendMessage(ChatColor.DARK_GREEN + "* You denied " + sender.getDisplayName() + "'s request.");
	sender.sendMessage(ChatColor.DARK_GREEN + "* " + player.getDisplayName() + " has denied your request.");

	Main.getInstance().removeTeleportRequest(player);
    }

    private void tpRequest(Player player, Player recv) {
	if (!Main.getInstance().getPlayer(recv).getTeleportToggle()) {
	    player.sendMessage(ChatColor.RED + "* This player has disabled teleporting to him/her.");
	    return;
	}

	player.sendMessage(ChatColor.RED + "* Your teleport request has been sent.");
	recv.sendMessage(ChatColor.DARK_GREEN + "* " + player.getDisplayName() + ChatColor.DARK_GREEN + " wants to teleport to you.");
	recv.sendMessage(ChatColor.DARK_GREEN + "* Use '/tp accept' to accept, or '/tp deny' to deny..");

        Main.getInstance().removeTeleportRequest(recv);
        Main.getInstance().addTeleportRequest(recv, player);
    }
}
